import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:opene/JhPickerTool.dart';
import 'package:opene/app_theme.dart';
import 'package:opene/app_utils.dart';
import 'package:opene/base/base_state.dart';
import 'package:opene/base/loading.dart';
import 'package:opene/base/scan_code.dart';
import 'package:opene/net/method/base_method.dart';
import 'package:opene/net/network_manager.dart';
import 'package:opene/net/response/base_response.dart';
import 'package:opene/ui/checkin/checkinverifypage.dart';
import 'package:opene/utils/ImageUtils.dart';
import 'package:toast/toast.dart';

import '../../app_constant.dart';
import 'checkinmodel.dart';

class CheckinPending extends StatelessWidget {
  CheckinInfo data;

  CheckinPending({this.data});

  @override
  Widget build(BuildContext context) {
    return CheckinPendingPage(checkinInfo: data);
  }
}

class CheckinPendingPage extends StatefulWidget {
  CheckinInfo checkinInfo;

  CheckinPendingPage({this.checkinInfo});

  @override
  _CheckinPendingPageState createState() => _CheckinPendingPageState(checkinInfo: checkinInfo);
}

class _CheckinPendingPageState extends BaseState<CheckinPendingPage> {
  CheckinInfo checkinInfo;

  _CheckinPendingPageState({this.checkinInfo});

  final TextEditingController dateController = TextEditingController();
  final controller = ScrollController();
  String containerNo;
  String typeValue;
  String uniqId;
  String strDate, tvContainerNoHint = "Container NO#: ";
  PhotoBean bolPhoto, doorPhoto, sealPhoto, openPhoto, licensePlatePhoto;
  List<PhotoBean> shippingLabels = new List();
  List<PhotoBean> bags = new List();
  List<PhotoBean> products = new List();
  List<PhotoBean> damages = new List();

  bool layoutBol = false,
      layoutDoor = false,
      layoutSeal = false,
      layoutOpen = false,
      layoutLicensePlate = false,
      layoutShippingLabel = false,
      layoutPackage = false,
      layoutPalletProduct = false,
      layoutDamage = false,
      tvDockHint = false,
      layoutDock = false,
      layoutType = false,
      ivLight = false;

  int index = 0, position;

  @override
  String setTitle({String title}) {
    return !Utils.isEmpty(title) ? title : "Checkin";
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      strDate = checkinInfo.dropDate;
      containerNo = checkinInfo.containerNo;
      typeValue = checkinInfo.type;
      uniqId = checkinInfo.uniqId;
      bolPhoto = new PhotoBean();
      if (checkinInfo.bol != null && checkinInfo.bol.length > 0) {
        bolPhoto.urlImg = checkinInfo.bol[0];
      }
      doorPhoto = new PhotoBean();
      if (checkinInfo.door != null) {
        doorPhoto.urlImg = checkinInfo.door;
      }
      sealPhoto = new PhotoBean();
      if (checkinInfo.seal != null) {
        sealPhoto.urlImg = checkinInfo.seal;
      }
      openPhoto = new PhotoBean();
      if (checkinInfo.open != null) {
        openPhoto.urlImg = checkinInfo.open;
      }
      licensePlatePhoto = new PhotoBean();
      if (checkinInfo.licensePlate != null) {
        licensePlatePhoto.urlImg = checkinInfo.licensePlate;
      }

      if (checkinInfo.shippingLabel != null && checkinInfo.shippingLabel.length > 0) {
        checkinInfo.shippingLabel.forEach((value) {
          if (!Utils.isEmpty(value)) {
            PhotoBean photoBeanTemp = new PhotoBean();
            photoBeanTemp.urlImg = value;
            shippingLabels.add(photoBeanTemp);
          }
        });
      }
      shippingLabels.add(new PhotoBean());

      if (checkinInfo.bag != null && checkinInfo.bag.length > 0) {
        checkinInfo.bag.forEach((value) {
          if (!Utils.isEmpty(value)) {
            PhotoBean photoBeanTemp = new PhotoBean();
            photoBeanTemp.urlImg = value;
            bags.add(photoBeanTemp);
          }
        });
      }
      bags.add(new PhotoBean());

      if (checkinInfo.product != null && checkinInfo.product.length > 0) {
        checkinInfo.product.forEach((value) {
          if (!Utils.isEmpty(value)) {
            PhotoBean photoBeanTemp = new PhotoBean();
            photoBeanTemp.urlImg = value;
            products.add(photoBeanTemp);
          }
        });
      }
      products.add(new PhotoBean());

      if (checkinInfo.damaged != null && checkinInfo.damaged.length > 0) {
        checkinInfo.damaged.forEach((value) {
          if (!Utils.isEmpty(value)) {
            PhotoBean photoBeanTemp = new PhotoBean();
            photoBeanTemp.urlImg = value;
            damages.add(photoBeanTemp);
          }
        });
      }
      damages.add(new PhotoBean());

      setVisibility();
    });
  }

  void setVisibility() {
    if (checkinInfo.operationMethod == "container") {
      layoutBol = true;
      layoutDoor = true;
      layoutSeal = true;
      layoutOpen = true;
      layoutLicensePlate = false;
      layoutShippingLabel = false;
      layoutPackage = false;
      layoutPalletProduct = false;
      layoutDamage = false;
      tvDockHint = true;
      layoutDock = true;
      layoutType = true;
      this.type = 1;
    } else if (checkinInfo.operationMethod == "truck") {
      tvContainerNoHint = "BOL NO#：";
      layoutBol = false;
      layoutDoor = false;
      layoutSeal = false;
      layoutOpen = false;
      layoutLicensePlate = true;
      layoutShippingLabel = false;
      layoutPackage = false;
      layoutPalletProduct = true;
      layoutDamage = true;
      layoutType = false;
      tvDockHint = false;
      layoutDock = false;
      this.type = 2;
    } else if (checkinInfo.operationMethod == "express") {
      tvContainerNoHint = "Tracking NO#：";
      layoutBol = false;
      layoutDoor = false;
      layoutSeal = false;
      layoutOpen = false;
      layoutLicensePlate = false;
      layoutShippingLabel = true;
      layoutPackage = true;
      layoutPalletProduct = true;
      layoutDamage = true;
      layoutType = false;
      tvDockHint = false;
      layoutDock = false;
      ivLight = true;
      this.type = 3;
    }
  }

  Future pickImages(context) async {
    File image = await ImageUtils.chosePhotoOnAlbumOrCamera(context: context);
    print(image.path);
    String name = "";
    setState(() {
      switch (index) {
        case 0:
          bolPhoto.image = image;
          name = "bol";
          break;
        case 1:
          doorPhoto.image = image;
          name = "door";
          break;
        case 2:
          sealPhoto.image = image;
          name = "seal";
          break;
        case 3:
          openPhoto.image = image;
          name = "open";
          break;
        case 4:
          licensePlatePhoto.image = image;
          name = "license_plate";
          break;
        case 5:
          PhotoBean photoBean = new PhotoBean();
          photoBean.image = image;
          shippingLabels.insert(position, photoBean);
          name = "shipping_label";
          break;
        case 6:
          PhotoBean photoBean = new PhotoBean();
          photoBean.image = image;
          bags.insert(position, photoBean);
          name = "package";
          break;
        case 7:
          PhotoBean photoBean = new PhotoBean();
          photoBean.image = image;
          products.insert(position, photoBean);
          name = "product";
          break;
        case 8:
          PhotoBean photoBean = new PhotoBean();
          photoBean.image = image;
          damages.insert(position, photoBean);
          name = "damage";
          break;
      }
      fileUpload(containerNo, "checkin", image, name, index, position, false, false);
      print("index:${index},position:${position}");
      print(image.path);
    });
  }

  @override
  Widget getViewBinding() {
    return Padding(
      padding: const EdgeInsets.only(left: 0, top: 10, right: 0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(left: 15),
            child: Row(
              children: [
                Text(
                  "Container NO#: ",
                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 14.0),
                ),
                Text(
                  Utils.getStringisEmpty(containerNo),
                  style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                ),
                Visibility(
                  visible: ivLight,
                  child: Icon(
                    Icons.arrow_forward_ios,
                    size: 15,
                  ),
                ),
              ],
            ),
          ),
          Visibility(
            visible: layoutType,
            child: Container(
              margin: EdgeInsets.only(left: 15),
              child: Row(
                children: [
                  Text(
                    "Type: ",
                    style: TextStyle(color: MyAppTheme.color_666666, fontSize: 14.0),
                  ),
                  Text(
                    Utils.getStringisEmpty(typeValue),
                    style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                  ),
                ],
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(left: 15),
            child: Row(
              children: [
                Text(
                  "Uniq ID: ",
                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 14.0),
                ),
                Text(
                  Utils.getStringisEmpty(uniqId),
                  style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                ),
              ],
            ),
          ),
          Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Expanded(
                flex: 1, //设置权重
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      padding: EdgeInsets.zero,
                      margin: EdgeInsets.zero,
                      height: 25.0,
                      child: Radio<int>(
                        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        value: 1,
                        groupValue: this.type,
                        activeColor: MyAppTheme.nearlyBlue,
                        onChanged: null,
                      ),
                    ),
                    Text(
                      'Container',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(color: this.type == 1 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 12.0),
                    ),
                  ],
                ),
              ),
              Expanded(
                flex: 1, //设置权重
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      padding: EdgeInsets.zero,
                      margin: EdgeInsets.zero,
                      height: 25.0,
                      child: Radio<int>(
                        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        value: 2,
                        groupValue: this.type,
                        activeColor: MyAppTheme.nearlyBlue,
                        onChanged: null,
                      ),
                    ),
                    Text(
                      'Truck',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(color: this.type == 2 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 12.0),
                    ),
                  ],
                ),
              ),
              Expanded(
                flex: 1, //设置权重
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      padding: EdgeInsets.zero,
                      margin: EdgeInsets.zero,
                      height: 25.0,
                      child: Radio<int>(
                        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        value: 3,
                        groupValue: this.type,
                        activeColor: MyAppTheme.nearlyBlue,
                        onChanged: null,
                      ),
                    ),
                    Text(
                      'Package',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(color: this.type == 3 ? MyAppTheme.nearlyBlue : MyAppTheme.color_333333, fontSize: 12.0),
                    ),
                  ],
                ),
              ),
            ],
          ),
          Visibility(
            visible: layoutDock,
            child: Container(
              margin: const EdgeInsets.only(left: 15, top: 5, right: 15, bottom: 0),
              width: double.infinity,
              child: ConstrainedBox(
                constraints: BoxConstraints(maxHeight: 40),
                child: TextField(
                  controller: textEditingController,
                  textInputAction: TextInputAction.search,
                  onSubmitted: (String text) {
                    print('Searching for $text');
                  },
                  // autofocus: true,
                  decoration: InputDecoration(
                    suffixIcon: IconButton(
                      icon: Icon(
                        Icons.qr_code_scanner,
                        size: 25,
                        color: MyAppTheme.nearlyBlue,
                      ),
                      onPressed: () {
                        // requestPermissions();
                        // Utils.scanBarcode(context);
                        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                          return ScanCode();
                        })).then((text) {
                          if (text == null) {
                            return;
                          }
                          print(text); //拿到扫描结果
                        });
                      },
                    ),
                    border: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: MyAppTheme.nearlyBlue,
                      ),
                      borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                    ),
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: MyAppTheme.nearlyBlue),
                      // 设置焦点之外的边框颜色
                      borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: MyAppTheme.nearlyBlue),
                      // 设置焦点时的边框颜色
                      borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                    ),
                    hintText: "please scan the dock num",
                    filled: true,
                    fillColor: MyAppTheme.white,
                    //隐藏下划线
                    //border: InputBorder.none,
                    hintStyle: TextStyle(color: Colors.grey),
                    contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 15),
                  ),
                  onChanged: (text) {
                    if (text == null) {
                      code = text;
                      return;
                    }
                    if ((code != null) && (code == text)) {
                      return;
                    }
                    code = text;
                  },
                ),
              ),
            ),
          ),
          Container(
            height: 40,
            margin: const EdgeInsets.only(left: 15, top: 3, right: 15, bottom: 0),
            width: double.infinity,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Text(
                  'Drop Date: ',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                ),
                Expanded(
                  flex: 1, //设置权重
                  child: GestureDetector(
                    onTap: () {
                      // 请求焦点
                      FocusScope.of(context).requestFocus(FocusNode());

                      JhPickerTool.showDatePicker(context, clickCallback: (var timeStr, var str) {
                        print(timeStr);
                        print(str);
                        setState(() {
                          strDate = timeStr;
                        });
                      });
                    },
                    child: Container(
                      height: 40,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(20.0)),
                        color: MyAppTheme.white,
                        border: Border.all(
                          color: MyAppTheme.nearlyBlue,
                          width: 1.0,
                        ),
                      ),
                      child: Row(
                        children: [
                          Container(
                            margin: const EdgeInsets.only(left: 0, top: 0, right: 9, bottom: 0),
                            width: 1,
                          ),
                          Expanded(
                            flex: 1, //设置权重
                            child: Text(
                              Utils.getStringisEmpty(strDate),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(color: MyAppTheme.color_333333),
                            ),
                          ),
                          Icon(
                            Icons.date_range,
                            size: 25,
                            color: MyAppTheme.nearlyBlue,
                          ),
                          Container(
                            margin: const EdgeInsets.only(left: 0, top: 0, right: 9, bottom: 0),
                            width: 1,
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            flex: 1, //设置权重
            child: SingleChildScrollView(
              controller: controller, // 设置控制器
              child: Column(
                children: [
                  Visibility(
                    visible: layoutBol,
                    child: setPhotoView("BOL:  ", 0, bolPhoto),
                  ),
                  Visibility(
                    visible: layoutDoor,
                    child: setPhotoView("Door:  ", 1, doorPhoto),
                  ),
                  Visibility(
                    visible: layoutSeal,
                    child: setPhotoView("Seal:  ", 2, sealPhoto),
                  ),
                  Visibility(
                    visible: layoutOpen,
                    child: setPhotoView("Open:  ", 3, openPhoto),
                  ),
                  Visibility(
                    visible: layoutLicensePlate,
                    child: setPhotoView("License plate:  ", 4, licensePlatePhoto),
                  ),
                  Visibility(
                    visible: layoutShippingLabel,
                    child: Container(
                      height: 70,
                      margin: const EdgeInsets.only(left: 0, top: 10, right: 0, bottom: 0),
                      width: double.infinity,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Expanded(
                            flex: 1, //设置权重
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                Text(
                                  '*',
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(color: MyAppTheme.red, fontSize: 12.0),
                                ),
                                Text(
                                  'Shipping label:  ',
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                                ),
                              ],
                            ),
                          ),
                          Expanded(
                            flex: 2, //设置权重
                            child: setListView(5, shippingLabels),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Visibility(
                    visible: layoutPackage,
                    child: Container(
                      height: 70,
                      margin: const EdgeInsets.only(left: 0, top: 10, right: 0, bottom: 0),
                      width: double.infinity,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Expanded(
                            flex: 1, //设置权重
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                Text(
                                  '*',
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(color: MyAppTheme.red, fontSize: 12.0),
                                ),
                                Text(
                                  'Package:  ',
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                                ),
                              ],
                            ),
                          ),
                          Expanded(
                            flex: 2, //设置权重
                            child: setListView(6, bags),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Visibility(
                    visible: layoutPalletProduct,
                    child: Container(
                      height: 70,
                      margin: const EdgeInsets.only(left: 0, top: 10, right: 0, bottom: 0),
                      width: double.infinity,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Expanded(
                            flex: 1, //设置权重
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                Text(
                                  '*\n ',
                                  maxLines: 2,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(color: MyAppTheme.red, fontSize: 12.0),
                                ),
                                Text(
                                  'Pallet(s)/\nProduct(s):  ',
                                  maxLines: 2,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                                ),
                              ],
                            ),
                          ),
                          Expanded(
                            flex: 2, //设置权重
                            child: setListView(7, products),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Visibility(
                    visible: layoutDamage,
                    child: Container(
                      height: 70,
                      margin: const EdgeInsets.only(left: 0, top: 10, right: 0, bottom: 0),
                      width: double.infinity,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Expanded(
                            flex: 1, //设置权重
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                Text(
                                  '*',
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(color: MyAppTheme.red, fontSize: 12.0),
                                ),
                                Text(
                                  'Damage(s):  ',
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                                ),
                              ],
                            ),
                          ),
                          Expanded(
                            flex: 2, //设置权重
                            child: setListView(8, damages),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Container setPhotoView(String hintText, int type, PhotoBean photoBean) {
    return Container(
      height: 70,
      margin: const EdgeInsets.only(left: 0, top: 10, right: 0, bottom: 0),
      width: double.infinity,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Expanded(
            flex: 1, //设置权重
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(
                  '*',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(color: MyAppTheme.red, fontSize: 12.0),
                ),
                Text(
                  hintText,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                ),
              ],
            ),
          ),
          Expanded(
              flex: 2, //设置权重
              child: Row(
                children: [
                  Container(
                    width: 70,
                    height: 70,
                    child: Stack(
                      children: <Widget>[
                        GestureDetector(
                          onTap: () {
                            // 请求焦点
                            FocusScope.of(context).requestFocus(FocusNode());
                            index = type;
                            ((photoBean.image == null) && Utils.isEmpty(photoBean.urlImg)) ? pickImages(context) : imgFileDialog(photoBean, Constants.baseImgUrl);
                          },
                          child: photoBean.image == null
                              ? (photoBean.urlImg == null
                                  ? Image.asset(
                                      'assets/images/add.png',
                                      width: 70,
                                      height: 70,
                                      fit: BoxFit.cover,
                                    )
                                  : Image.network(
                                      Constants.baseImgUrl + photoBean.urlImg,
                                      width: 70,
                                      height: 70,
                                      fit: BoxFit.cover,
                                    ))
                              : Image.file(
                                  photoBean.image,
                                  width: 70,
                                  height: 70,
                                  fit: BoxFit.cover,
                                ),
                        ),
                        Visibility(
                          visible: ((photoBean.image == null) && Utils.isEmpty(photoBean.urlImg)) ? false : true,
                          child: Positioned(
                            top: 2.0,
                            right: 2.0,
                            child: GestureDetector(
                              onTap: () {
                                setState(() {
                                  photoBean.urlImg = null;
                                  photoBean.image = null;
                                });
                              },
                              child: Image.asset(
                                'assets/images/ic_delete_menu.png',
                                width: 15,
                                height: 15,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Expanded(
                    flex: 2, //设置权重
                    child: Text(
                      '',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(color: MyAppTheme.red, fontSize: 12.0),
                    ),
                  ),
                ],
              )),
        ],
      ),
    );
  }

  ListView setListView(int type, List<PhotoBean> data) {
    return ListView.builder(
      scrollDirection: Axis.horizontal,
      itemCount: data == null ? 0 : data.length,
      itemBuilder: (context, index) {
        PhotoBean photoBean = data[index];
        print(photoBean.toString());
        return Container(
          height: 70,
          width: 70,
          margin: const EdgeInsets.only(left: 0, top: 0, right: 10, bottom: 0),
          child: Stack(
            children: <Widget>[
              GestureDetector(
                onTap: () {
                  // 请求焦点
                  FocusScope.of(context).requestFocus(FocusNode());
                  this.index = type;
                  this.position = index;
                  ((photoBean.image == null) && Utils.isEmpty(photoBean.urlImg)) ? pickImages(context) : imgFileDialog(photoBean, Constants.baseImgUrl);
                },
                child: photoBean.image == null
                    ? (photoBean.urlImg == null
                        ? Image.asset(
                            'assets/images/add.png',
                            width: 70,
                            height: 70,
                            fit: BoxFit.cover,
                          )
                        : Image.network(
                            Constants.baseImgUrl + photoBean.urlImg,
                            width: 70,
                            height: 70,
                            fit: BoxFit.cover,
                          ))
                    : Image.file(
                        photoBean.image,
                        width: 70,
                        height: 70,
                        fit: BoxFit.cover,
                      ),
              ),
              Visibility(
                visible: ((photoBean.image == null) && Utils.isEmpty(photoBean.urlImg)) ? false : true,
                child: Positioned(
                  top: 2.0,
                  left: 53,
                  child: GestureDetector(
                    onTap: () {
                      setState(() {
                        data.removeAt(index);
                      });
                    },
                    child: Image.asset(
                      'assets/images/ic_delete_menu.png',
                      width: 15,
                      height: 15,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  void showFileUploadData(String path, int type, int position, bool isSubmit) {
    switch (type) {
      case 0:
        bolPhoto.urlImg = path;
        break;
      case 1:
        doorPhoto.urlImg = path;
        break;
      case 2:
        sealPhoto.urlImg = path;
        break;
      case 3:
        openPhoto.urlImg = path;
        break;
      case 4:
        licensePlatePhoto.urlImg = path;
        break;
      case 5:
        shippingLabels[position].urlImg = path;
        break;
      case 6:
        bags[position].urlImg = path;
        break;
      case 7:
        products[position].urlImg = path;
        break;
      case 8:
        damages[position].urlImg = path;
        break;
    }

    if (isSubmit) {
      submitData();
    }
  }

  @override
  void setNext() {
    ToastContext().init(context);
    if (Utils.isEmpty(strDate)) {
      Toast.show("Please select Drop Date", duration: Toast.lengthShort, gravity: Toast.bottom);
      return;
    }
    submitData();
  }

  void submitData() {
    CheckinPendingQr checkinPendingQr = new CheckinPendingQr();
    checkinPendingQr.id = checkinInfo.id;
    checkinPendingQr.dock = checkinInfo.checkinDock;
    checkinPendingQr.dropDate = strDate;

    if (checkinInfo.operationMethod == "container") {
      code = textEditingController.text;
      if (Utils.isEmpty(code)) {
        Toast.show("please scan the dock num", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }
      checkinPendingQr.dock = code;

      if (bolPhoto.image == null) {
        Toast.show("Please add bol photos", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }
      if (doorPhoto.image == null) {
        Toast.show("Please add door photos", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }
      if (sealPhoto.image == null) {
        Toast.show("Please add seal photos", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }
      if (openPhoto.image == null) {
        Toast.show("Please add open photos", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }
      if (Utils.isEmpty(bolPhoto.urlImg)) {
        fileUpload(containerNo, "checkin", bolPhoto.image, "bol", 0, position, true, true);
        return;
      }
      if (Utils.isEmpty(doorPhoto.urlImg)) {
        fileUpload(containerNo, "checkin", doorPhoto.image, "door", 1, position, true, true);
        return;
      }
      if (Utils.isEmpty(sealPhoto.urlImg)) {
        fileUpload(containerNo, "checkin", sealPhoto.image, "seal", 2, position, true, true);
        return;
      }
      if (Utils.isEmpty(openPhoto.urlImg)) {
        fileUpload(containerNo, "checkin", openPhoto.image, "open", 3, position, true, true);
        return;
      }
      checkinPendingQr.bol.add(bolPhoto.urlImg);
      checkinPendingQr.door = doorPhoto.urlImg;
      checkinPendingQr.seal = sealPhoto.urlImg;
      checkinPendingQr.open = openPhoto.urlImg;
      ;
    } else if (checkinInfo.operationMethod == "truck") {
      if (licensePlatePhoto.image == null) {
        Toast.show("Please add license plate photos", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }
      if (products.length < 2) {
        Toast.show("Please add product photo", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }

      if (Utils.isEmpty(licensePlatePhoto.urlImg)) {
        fileUpload(containerNo, "checkin", licensePlatePhoto.image, "license_plate", 4, position, true, true);
        return;
      }

      checkinPendingQr.licensePlate = licensePlatePhoto.urlImg;

      for (int i = 0; i < products.length - 1; i++) {
        PhotoBean photoBean = products[i];

        if (Utils.isEmpty(photoBean.urlImg)) {
          position = i;
          fileUpload(containerNo, "checkin", photoBean.image, "product", 7, position, true, true);
          return;
        }
      }

      List<String> damagesTemp = new List();
      for (int i = 0; i < damages.length - 1; i++) {
        PhotoBean photoBean = damages[i];
        if (Utils.isEmpty(photoBean.urlImg)) {
          position = i;
          fileUpload(containerNo, "checkin", photoBean.image, "damage", 8, position, true, true);
          return;
        }
        damagesTemp.add(photoBean.urlImg);
      }
      checkinPendingQr.damaged = damagesTemp;

    } else if (checkinInfo.operationMethod == "express") {
      if (shippingLabels.length < 2) {
        Toast.show("Please add shipping label photos", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }

      if (bags.length < 2) {
        Toast.show("Please add package photos", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }

      if (products.length < 2) {
        Toast.show("Please add product photos", duration: Toast.lengthShort, gravity: Toast.bottom);
        return;
      }

      List<String> shippingLabelsTemp = new List();
      for (int i = 0; i < shippingLabels.length - 1; i++) {
        PhotoBean photoBean = shippingLabels[i];
        if (Utils.isEmpty(photoBean.urlImg)) {
          position = i;
          fileUpload(containerNo, "checkin", photoBean.image, "shipping_label", 5, position, true, true);
          return;
        }
        shippingLabelsTemp.add(photoBean.urlImg);
      }
      checkinPendingQr.shippingLabel = shippingLabelsTemp;

      List<String> bagsTemp = new List();
      for (int i = 0; i < bags.length - 1; i++) {
        PhotoBean photoBean = bags[i];
        if (Utils.isEmpty(photoBean.urlImg)) {
          position = i;
          fileUpload(containerNo, "checkin", photoBean.image, "package", 6, position, true, true);
          return;
        }
        bagsTemp.add(photoBean.urlImg);
      }
      checkinPendingQr.bag = bagsTemp;

      List<String> productsTemp = new List();
      for (int i = 0; i < products.length - 1; i++) {
        PhotoBean photoBean = products[i];
        if (Utils.isEmpty(photoBean.urlImg)) {
          position = i;
          fileUpload(containerNo, "checkin", photoBean.image, "product", 7, position, true, true);
          return;
        }
        productsTemp.add(photoBean.urlImg);
      }
      checkinPendingQr.product = productsTemp;

      List<String> damagesTemp = new List();
      for (int i = 0; i < damages.length - 1; i++) {
        PhotoBean photoBean = damages[i];
        if (Utils.isEmpty(photoBean.urlImg)) {
          position = i;
          fileUpload(containerNo, "checkin", photoBean.image, "damage", 8, position, true, true);
          return;
        }
        damagesTemp.add(photoBean.urlImg);
      }
      checkinPendingQr.damaged = damagesTemp;
    }
    checkSubmit(checkinInfo, checkinPendingQr);
  }

  @override
  void setReset() {
    setState(() {
      textEditingController.text = "";
      strDate = "";
      bolPhoto = new PhotoBean();
      doorPhoto = new PhotoBean();
      sealPhoto = new PhotoBean();
      openPhoto = new PhotoBean();
      licensePlatePhoto = new PhotoBean();
      shippingLabels.clear();
      shippingLabels.add(new PhotoBean());
      bags.clear();
      bags.add(new PhotoBean());
      products.clear();
      products.add(new PhotoBean());
      damages.clear();
      damages.add(new PhotoBean());
    });
  }

  void checkSubmit(CheckinInfo checkinInfo, CheckinPendingQr checkinPendingQr) async {
    String json = jsonEncode(checkinPendingQr);
    try {
      // 显示加载中对话框
      LoadingIndicator.show(context);
    } catch (e) {
      e.toString();
    }
    BaseResponse baseResponse = await NetworkManager().request(context, Constants.checkSubmit, method: BaseMethod.post, data: json);

    try {
      // 在需要的时候隐藏加载中对话框
      LoadingIndicator.hide();
    } catch (e) {
      e.toString();
    }

    if (baseResponse.status == 200) {
      CheckinPendingModel checkinPending = CheckinPendingModel.fromJson(baseResponse.data);
      if (checkinInfo.operationMethod == "container") {
        Navigator.pop(context, true);
      } else {
        Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => CheckinVerify(
                    data: checkinInfo,
                    checkinPending: checkinPending,
                  )),
        ).then((value) => {Navigator.pop(context, value)});
      }
    } else {
      ToastContext().init(context);
      Toast.show(Utils.getStringisEmpty(baseResponse.msg), duration: Toast.lengthShort, gravity: Toast.bottom);
    }
  }
}
